AddCSLuaFile( "cl_channels.lua" )
AddCSLuaFile( "gui/channel_elements.lua" )
AddCSLuaFile( "gui/channel_win.lua" )

Channels = {}
Channels.List = {}

include( "sv_talkfunc.lua" )

--We can use a fake player to send our messages to radio channels
local FakePlayer = {}
function FakePlayer:Name() return "ChanServ" end
function FakePlayer:Team() return 0 end


--This bit is a bit hard to read
hook.Add("PlayerSay", "Channels.Chat", function( ply, msg, team )
	if not ValidEntity( ply ) then return end
	
	local Spl = string.Explode( " ", msg )
	
	local Cmd = string.Trim( string.lower( Spl[1] or "" ) )
	local Chan = string.Trim( string.lower( Spl[2] or "" ) )
	local Pass = string.Trim( string.lower( Spl[3] or "" ) )
	
	if Cmd == "/channels" then
		ply:ConCommand( "chan_show" )
		return ""
	elseif Cmd == "/radio" or Cmd == "/r"  then
		if not ply.Channel or ply.Channel == "" then
			ply:ChatPrint( "You're not in a channel! Type \"/channels\" to open the channels window." )
			return ""
		end
		
		Spl[1] = "" --Clear this entry so we don't send the entire message
		Channels:ChanSay( ply, string.sub(string.Implode(" ", Spl), 2), ply.Channel )
		
		return ""
	elseif Cmd == "/joinchan" or Cmd == "/channel" then
		ply:ChatPrint( "Type \"/channels\" to view the channel GUI." )
		return ""
	elseif Cmd == "/nochan" or Cmd == "/leavechan" then
		local OldChannel = ply.Channel or ""
		if OldChannel and OldChannel ~= "" then
			ply.Channel = ""
			Channels:ChanSay( FakePlayer, ply:Name() .. " has left the channel", OldChannel )
			ply:ChatPrint( "You have left the channel" )
			
			Channels:OnLeave( OldChannel )
			Channels:Notify( ply )
		else
			ply:ChatPrint( "You're not in a channel!" )
		end
		
		return ""
	elseif Cmd == "/who" then
		if not ply.Channel or ply.Channel == "" then
			ply:ChatPrint( "You're not in a channel!" )
			return ""
		end
		
		local Players = {}
		for k, v in pairs( player.GetAll() ) do
			if v.Channel == ply.Channel then
				table.insert( Players, v )
			end
		end
		
		local str = "In this channel: "
		for k, v in pairs( Players ) do
			if #Players > 1 then
				if k == #Players - 1 then
					str = str .. v:Name() .. " and "
				elseif k ~= #Players
					str = str .. v:Name() .. ", "
				else
					str = str .. v:Name() .. "."
				end
			else
				str = str .. v:Name() .. ". "
			end
		end
		ply:ChatPrint( str )
		
		return ""
	elseif Cmd == "//" then
		return "(OOC): " .. string.sub(msg, 4)
	elseif not (GetConVarNumber("sv_alltalk") == 1) then
		local Range = 400
		local Prefix = ""
		local Message = msg
		
		local Spl2 = string.Explode( " ", string.lower(msg) )
		if Spl2[1] == "/y" then
			Range = 700
			Prefix = "(yell) "
			Message = string.upper(string.sub(msg, 4))
		elseif Spl2[1] == "/w" then
			Range = 100
			Prefix = "(whisper) "
			Message = string.sub(msg, 4)
		end
		
		TalkRanged( ply, Prefix .. Message, Range )
		return ""
	end
	
end)


concommand.Add( "chan_join", function( ply, cmd, args )
	if not ValidEntity( ply ) then return end
	if #args < 1 then
		ply:ChatPrint( "chan_join takes an argument, foolish noob! Add one or Mr. T will start one." )
		return
	end
	
	local Channel = string.Trim( string.lower( args[1] ) )
	local Pass = string.Trim( string.lower( args[2] ) )
	
	if #Channel > 7 then
		ply:ChatPrint( "Channel IDs are only 7 characters max." )
		return
	elseif #Pass > 20 then
		ply:ChatPrint( "Passwords are only 20 characters max." )
		return
	end
	
	if not Channels.List[ Channel ] then
		ply:ChatPrint( "That channel doesn't exist! Type \"/channels\" to create one." )
		return
	elseif Channels.List[ Channel ][2] ~= Pass then
		ply:ChatPrint( "Bad password!" )
		return
	end
	
	if ply.Channel == Channel then
		ply:ChatPrint( "You're already in this channel you doughnut" )
		return
	end
	
	if ply.Channel and ply.Channel ~= "" then
		Channels:ChanSay( FakePlayer, ply:Name() .. " has left the channel", ply.Channel )
	end
	
	ply.Channel = Channel
	
	Channels:ChanSay( FakePlayer, ply:Name() .. " has joined the channel!", Channel )
	Channels:Notify( ply )
end )


concommand.Add( "chan_leave", function( ply, cmd, args )
	if not ValidEntity( ply ) then return end
	
	if not ply.Channel or ply.Channel == "" then
		ply:ChatPrint( "You're not in a channel! Type \"/channels\" to open the channels window." )
		return ""
	else
		Channels:ChanSay( FakePlayer, ply:Name() .. " has left the channel", ply.Channel )
		ply.Channel = ""
	end
end )


concommand.Add( "chan_create", function( ply, cmd, args )
	if not ValidEntity( ply ) then return end
	if #args < 2 then
		ply:ChatPrint( "chan_create takes another argument, foolish noob! Add one or Mr. T will start one." )
		return
	end
	
	local Name = string.Trim( args[1] )
	
	local Channel = string.Trim( string.lower( args[2] ) )
	if string.find( Channel, " " ) then
		ply:ChatPrint( "Channel IDs can't contain any spaces!" )
		return
	end
	
	local Password = string.Trim( string.lower( args[3] or "" ) )
	if string.find( Password, " " ) then
		ply:ChatPrint( "Passwords can't contain any spaces!" )
		return
	end
	
	if #Name > 60 then
		ply:ChatPrint( "That's a huge name for a channel. 60 characters max." )
		return
	elseif #Channel > 7 then
		ply:ChatPrint( "Channel IDs should be 7 characters max." )
		return
	elseif #Password > 20 then
		ply:ChatPrint( "Passwords should be 20 characters max." )
		return
	elseif #Name <= 1 or #Channel <= 1 then
		ply:ChatPrint( "Blank name or channel!" )
		return
	end
	
	if Channels.List[ Channel ] ~= nil then
		ply:ChatPrint( "That channel already exists." )
		return
	end
	
	local PassStr = "!"
	if Password ~= "" then
		PassStr = " with password \"" .. Password .. "\"!"
	end
	
	Channels.List[ Channel ] = { Name, Password }
	
	ply:ChatPrint( "Your channel \"" .. Name .. "\" has been created" .. PassStr )
	ply:ChatPrint( "This is all lowercase! Remember to join your new channel." )
	
	Channels:TellClients( Name, Channel )
end )